package com.zhou.listener;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.liulishuo.filedownloader.FileDownloader;
import com.zhou.listener.bean.KeyGen;
import com.zhou.listener.library.ListenerManager;
import com.zhou.listener.baselistener.DetailFileDownloadListener;
import com.zhou.listener.library.utils.KeyUtils;
import com.zhou.listener.listener.MyAdapterListener;
import com.zhou.listener.utils.AdapterUtils;
import com.zhou.listener.library.utils.IKey;
import com.zhou.listener.library.vh.BaseDownloadViewHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 这样全部在Adapter中做文章，就可以完成实时更新操作啦。
 */
public class MyAdapter extends BaseAdapter {
    Context context;
    ArrayList<MyBean> list;
    String path;
    public MyAdapter(Context ctx,ArrayList<MyBean> myBeans){
        this.context = ctx;
        this.list = myBeans==null?new ArrayList<MyBean>():myBeans;
        path = context.getCacheDir().getPath()+File.separator + "image_manager_disk_cache";
        new File(path).mkdirs();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if(convertView!=null){
            vh = (ViewHolder) convertView.getTag();
            //第一步移除以前的key
            AdapterUtils.clearPreListener(vh.getKeys());
        }else{
            View v = LayoutInflater.from(context).inflate(R.layout.item,null,false);
            vh = new ViewHolder();
            vh.name = v.findViewById(R.id.name);
            v.setTag(vh);
            convertView = v;
        }
        //第二部创建新Key
        KeyGen key = new KeyGen(list.get(position).id+"");
        //第三部，把新key放进vh中
        KeyUtils.addKeyGens(vh,key);
        //第四部，根据新key更新listener
        final MyAdapterListener myAdapterListener = new MyAdapterListener(vh);
        myAdapterListener.initState(0);
        AdapterUtils.updateListener(vh.keys,myAdapterListener);
//        ((DetailFileDownloadListener)ListenerManager.getInstance().getListener(key)).getFileDownloaderListener().initState(0);
        //第五步，根据外面得到的状态来进行初始化state的创建

//        vh.name.setText(list.get(position).name);
        final ViewHolder finalVh = vh;
        vh.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position==list.size()-1){
                    context.startActivity(new Intent(context,RecyclerViewActivity.class));
                    return;
                }
                //第五步，按照规则在下载时设置listener
                FileDownloader.getImpl().create(list.get(position).url)
                        .setPath(path)
                        .setForceReDownload(true)
                        .setListener(ListenerManager.getInstance().addListener(finalVh.keys.get(0),
                                new DetailFileDownloadListener(myAdapterListener)
                        )).start();
            }
        });
        return convertView;
    }

    //开始步骤，创建viewHolder时需要实现baseViewHolder
    public class ViewHolder implements BaseDownloadViewHolder {
        public Button name;
        public List<IKey> keys;
        @Override
        public List<IKey> getKeys() {
            return keys;
        }
        @Override
        public void setKeys(List<IKey> keys) {
            this.keys = keys;
        }
    }
}
